package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.system.domain.PetInfo;
import com.ruoyi.system.mapper.PetInfoMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PetPublishMapper;
import com.ruoyi.system.domain.PetPublish;
import com.ruoyi.system.service.IPetPublishService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 宠物发布Service业务层处理
 */
@Service
public class PetPublishServiceImpl implements IPetPublishService {
    @Autowired
    private PetPublishMapper petPublishMapper;

    @Autowired
    private PetInfoMapper petInfoMapper;

    /**
     * 查询宠物发布
     * 
     * @param publishId 宠物发布ID
     * @return 宠物发布
     */
    @Override
    public PetPublish selectPetPublishById(Long publishId) {
        return petPublishMapper.selectPetPublishById(publishId);
    }

    /**
     * 查询宠物发布列表
     * 
     * @param petPublish 宠物发布
     * @return 宠物发布
     */
    @Override
    public List<PetPublish> selectPetPublishList(PetPublish petPublish) {
        return petPublishMapper.selectPetPublishList(petPublish);
    }

    /**
     * 新增宠物发布
     * 
     * @param petPublish 宠物发布
     * @return 结果
     */
    @Override
    public int insertPetPublish(PetPublish petPublish) {
        petPublish.setCreateTime(DateUtils.getNowDate());
        petPublish.setPublisherId(SecurityUtils.getUserId());
        petPublish.setPublisherName(SecurityUtils.getUsername());
        petPublish.setStatus("0"); // 设置初始状态为待审核
        return petPublishMapper.insertPetPublish(petPublish);
    }

    /**
     * 修改宠物发布
     * 
     * @param petPublish 宠物发布
     * @return 结果
     */
    @Override
    public int updatePetPublish(PetPublish petPublish) {
        petPublish.setUpdateTime(DateUtils.getNowDate());
        return petPublishMapper.updatePetPublish(petPublish);
    }

    /**
     * 批量删除宠物发布
     * 
     * @param publishIds 需要删除的宠物发布ID
     * @return 结果
     */
    @Override
    public int deletePetPublishByIds(Long[] publishIds) {
        return petPublishMapper.deletePetPublishByIds(publishIds);
    }

    /**
     * 删除宠物发布信息
     * 
     * @param publishId 宠物发布ID
     * @return 结果
     */
    @Override
    public int deletePetPublishById(Long publishId) {
        return petPublishMapper.deletePetPublishById(publishId);
    }

    @Override
    public int review(PetPublish petPublish) {
        petPublish.setApproveBy(SecurityUtils.getUsername());
        petPublish.setApproveTime(new Date());
        if(petPublish.getStatus() != null && Objects.equals(petPublish.getStatus(), "1")){
            petPublish.setPublishTime(new Date());
        }
        PetPublish publish = petPublishMapper.selectPetPublishById(petPublish.getPublishId());
        if(ObjectUtils.isEmpty(publish)){
            throw new BaseException("申请信息不存在");
        }
        int i = petPublishMapper.updatePetPublish(petPublish);
        if(petPublish.getStatus() != null && petPublish.getStatus().equals("1")){
            PetInfo petInfo = new PetInfo();
            petInfo.setStatus("0");
            petInfo.setAge(publish.getAge());
            petInfo.setGender(publish.getGender());
            petInfo.setDescription(publish.getDescription());
            petInfo.setCategoryName(publish.getPetTypeName());
            petInfo.setName(publish.getPetName());
            petInfo.setImage(publish.getImage());
            petInfo.setCategoryId(publish.getPetType());
            petInfoMapper.insertPetInfo(petInfo);
        }
        return i;
    }
} 